Java lastIndexOf() method

    lastIndexOf() method

    You can get the last index value of the provided character or the substring using this method. The index value will start from zero.

    Method implementation-

    public int lastIndexOf(int ch) {  
    
           return lastIndexOf(ch, value.length - 1);  
    
       }  

    Syntax-

    int lastIndexOf(int ch)

    It will return the last index position for the given char value

    int lastIndexOf(int ch, int fromIndex)

    It will return the last index position for the given char value and from index

    int lastIndexOf(String substring)

    It will return the last index position for the given substring

    int lastIndexOf(String substring, int fromIndex)

    It will return the last index position for the given substring and from the index.

    Example-

    public class Simple{  
        public static void main(String[] args) { 
          String s1="welcome to Java";
          int ind=s1.lastIndexOf("t");
          System.out.println(ind);
        }
    }  

    Output-

    Java Last index

    Example-

    public class Simple{  
        public static void main(String[] args) {      
            String s1="welcome to Java";
            int ind=s1.lastIndexOf("t",3);
            System.out.println(ind);    
        }
    }  

    Output-

    Java Last index of

    Example-

    public class Simple{  
        public static void main(String[] args) {      
           String s1="welcome to Java";
           int ind=s1.lastIndexOf("to");
           System.out.println(ind);    
        }
    }